package com.ews.onlineexchange.app;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.Notification;
import com.ews.onlineexchange.model.User;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class NotificationListForApp extends EntityQuery<Notification> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userId;
	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Notification> entity) {
		List<Predicate> restrictions = super.getRestrictions(cb, entity);
		Join<Notification, User> loginedUser = entity.join("receiver");
		restrictions.add(cb.equal(loginedUser.get("id"), userId));
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		String title = (String)getParameterValues().get("title");
		if(StringUtils.isNotBlank(title)) {
			restrictions.add(cb.like(entity.get("titile"), getStringRestriction(title)));
		}
		return restrictions;
	}

	public NotificationListForApp() {
		setMaxResults(MAX_RESULTS_FEW);
		setOrder("hasRead ,sendDate desc");
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	

}
